package com.lmk.org.service.Impl;

import com.lmk.basic.exception.BusinessException;
import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.basic.util.BaiduAuditUtils;
import com.lmk.org.domain.Employee;
import com.lmk.org.domain.Shop;
import com.lmk.org.domain.ShopAuditLog;
import com.lmk.org.dto.ShopDto;
import com.lmk.org.mapper.EmployeeMapper;
import com.lmk.org.mapper.ShopAuditLogMapper;
import com.lmk.org.mapper.ShopMapper;
import com.lmk.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1.空值校验,判断店铺名是否为空

        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名不能为空");
        }
        //2.判断该店铺是否已经注册过，通过店铺名和地址是否存在
        Shop shopTemp = shopMapper.loadByNameAndAddress(shop);
        if (shopTemp != null) {
            throw new BusinessException("店铺已经注册过了，请总直接登录");
        }
        // 百度ai智能审核
        // 审核店铺名和地址
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());
        if (!aBoolean) {
            throw new BusinessException("店铺名或地址不合法！！");
        }
        // 审核代店铺logo是否合法
        if (!StringUtils.isEmpty(shop.getLogo())) {
            String url = "http://123.207.27.208" + shop.getLogo(); //常量
            Boolean aBoolean1 = BaiduAuditUtils.ImgCensor(url);
            if (!aBoolean1) {
                throw new BusinessException("店铺头像不合法！！");
            }
        }


        //3.保存店铺管理员信息t_employee
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);
        //4.保存店铺信息t_shop
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);
        //5.将店铺的id更新到t_employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override// 审核通过
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态 id

        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        // 2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.save(shopAuditLog);

        // 3.发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 设置发送邮件的账号
        mimeMessageHelper.setFrom("1767442215@qq.com");
        // 设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        mimeMessageHelper.setTo(employee.getEmail());
        //设置主题 http://localhost:8080/shop/active/
        mimeMessageHelper.setSubject("店铺注册成功激活邮件");
        mimeMessageHelper.setText("<h1>恭喜！您的店铺注册成功啦</h1><h3>" +
                "<a href='http://localhost:8080/shop/active/"+shopAuditLog.getShopId()+"'>点击该链接激活店铺</a></h3>", true);
        javaMailSender.send(mimeMessage);
    }

    @Override// 不通过
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        // 2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.save(shopAuditLog);

        // 3.发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        // 设置发送邮件的账号
        mimeMessageHelper.setFrom("1767442215@qq.com");
        // 设置收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        mimeMessageHelper.setTo(employee.getEmail());
        //设置主题
        mimeMessageHelper.setSubject("店铺注册失败邮件");
        mimeMessageHelper.setText("<h1>抱歉！您的店铺注册失败，点击链接重新开始注册</h1><h3>" +
                "<a href='http://localhost:8081/#/register'>点击该链接重新注册店铺</a></h3>", true);
        javaMailSender.send(mimeMessage);

    }

    // 将导入的excel数据批量添加到数据库
    @Override
    public void addPatch(List<Shop> shops) {
        shopMapper.addPatch(shops);
    }

    // 通过状态来获取数量
    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }
}
